package io.renren.common.utils;

//import com.qcloud.cos.http.RequestHeaderValue;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author: xiaomi
 * @date: 2021/5/26
 * @description:
 */
public class HttpUtils {
    public static final String DEFAULT_CHARSET = "UTF-8";

    public HttpUtils() {
    }

    public static String doGet(String url, Map<String, String> param) throws IOException, URISyntaxException {
        return doGet(url, param, "UTF-8", UserAgent.gerRandomUserAgent());
    }

    public static String doGet(String url, Map<String, String> param, String charset) throws IOException, URISyntaxException {
        return doGet(url, param, charset, UserAgent.gerRandomUserAgent());
    }

    public static String doGet(String url, Map<String, String> param, String charset, String userAgent) throws IOException, URISyntaxException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String resultString = "";
        CloseableHttpResponse response = null;

        try {
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                Iterator var8 = param.keySet().iterator();

                while (var8.hasNext()) {
                    String key = (String) var8.next();
                    builder.addParameter(key, (String) param.get(key));
                }
            }

            URI uri = builder.build();
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setHeader("User-Agent", userAgent);
            response = httpclient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), charset);
            }
        } catch (Exception var17) {
            throw var17;
        } finally {
            try {
                if (response != null) {
                    response.close();
                }

                httpclient.close();
            } catch (IOException var16) {
                throw var16;
            }
        }

        return resultString;
    }

    public static String doGetNotRedirect(String url, Map<String, String> param) throws IOException, URISyntaxException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setRedirectsEnabled(false).build();
        String resultString = "";
        CloseableHttpResponse response = null;

        try {
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                Iterator var7 = param.keySet().iterator();

                while (var7.hasNext()) {
                    String key = (String) var7.next();
                    builder.addParameter(key, (String) param.get(key));
                }
            }

            URI uri = builder.build();
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6");
            httpGet.setConfig(requestConfig);
            response = httpclient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            } else if (response.getStatusLine().getStatusCode() == 302) {
                Header header = response.getFirstHeader("location");
                resultString = header.getValue();
            }
        } catch (Exception var17) {
            throw var17;
        } finally {
            try {
                if (response != null) {
                    response.close();
                }

                httpclient.close();
            } catch (IOException var16) {
                throw var16;
            }
        }

        return resultString;
    }

    public static int getCodeByGet(String url, Map<String, String> param, String userAgent) throws IOException, URISyntaxException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();
        CloseableHttpResponse response = null;

        int var9;
        try {
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                Iterator var7 = param.keySet().iterator();

                while (var7.hasNext()) {
                    String key = (String) var7.next();
                    builder.addParameter(key, (String) param.get(key));
                }
            }

            URI uri = builder.build();
            HttpGet httpGet = new HttpGet(uri);
            if (StringUtils.isBlank(userAgent)) {
                userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
            }

            httpGet.setHeader("User-Agent", userAgent);
            httpGet.setConfig(requestConfig);
            response = httpclient.execute(httpGet);
            var9 = response.getStatusLine().getStatusCode();
        } catch (Exception var18) {
            throw var18;
        } finally {
            try {
                if (response != null) {
                    response.close();
                }

                httpclient.close();
            } catch (IOException var17) {
                throw var17;
            }
        }

        return var9;
    }

    public static int getDirectCodeByGet(String url, Map<String, String> param, String userAgent) throws IOException, URISyntaxException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setRedirectsEnabled(false).setSocketTimeout(10000).setConnectTimeout(10000).build();
        CloseableHttpResponse response = null;

        int var9;
        try {
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                Iterator var7 = param.keySet().iterator();

                while (var7.hasNext()) {
                    String key = (String) var7.next();
                    builder.addParameter(key, (String) param.get(key));
                }
            }

            URI uri = builder.build();
            HttpGet httpGet = new HttpGet(uri);
            if (StringUtils.isBlank(userAgent)) {
                userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
            }

            httpGet.setHeader("User-Agent", userAgent);
            httpGet.setConfig(requestConfig);
            response = httpclient.execute(httpGet);
            var9 = response.getStatusLine().getStatusCode();
        } catch (Exception var18) {
            throw var18;
        } finally {
            try {
                if (response != null) {
                    response.close();
                }

                httpclient.close();
            } catch (IOException var17) {
                throw var17;
            }
        }

        return var9;
    }

    public static String doPost(String url, Map<String, String> param, String charset, int timeout) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();


        CloseableHttpResponse response = null;
        String resultString = "";

        try {
            HttpPost httpPost = new HttpPost(url);
            if (timeout > 0) {
                RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
                httpPost.setConfig(requestConfig);
            }
            if (param != null) {
                List<NameValuePair> paramList = new ArrayList();
                Iterator var8 = param.keySet().iterator();

                while (var8.hasNext()) {
                    String key = (String) var8.next();
                    paramList.add(new BasicNameValuePair(key, (String) param.get(key)));
                }

                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
                httpPost.setEntity(entity);
            }

            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), charset);
        } catch (Exception var17) {
            throw var17;
        } finally {
            try {
                response.close();
            } catch (IOException var16) {
                throw var16;
            }
        }

        return resultString;
    }

    public static String doPost(String url, String json) throws IOException {
        return doPost(url, json, "UTF-8", "");
    }

    public static String doPost(String url, Map<String, String> param, int timeout) throws IOException {
        return doPost(url, param, "UTF-8", timeout);
    }

    public static String doPost4Fiddler(String url, String json) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http");
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        httpClient = HttpClients.custom().setRoutePlanner(routePlanner).build();
        CloseableHttpResponse response = null;
        String resultString = "";

        try {
            HttpPost httpPost = new HttpPost(url);
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception var17) {
            var17.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException var16) {
                throw var16;
            }
        }

        return resultString;
    }

    public static String doPost(String url, String json, String charset, String userAgent) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";

        try {
            HttpPost httpPost = new HttpPost(url);
            if (StringUtils.isBlank(userAgent)) {
                userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
            }
            httpPost.setHeader("User-Agent", userAgent);
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), charset);
        } catch (Exception var17) {
            var17.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException var16) {
                throw var16;
            }
        }

        return resultString;
    }

    public static String doPut(String url, Map<String, String> param) throws IOException, URISyntaxException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String resultString = "";
        CloseableHttpResponse response = null;

        try {
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                Iterator var6 = param.keySet().iterator();

                while (var6.hasNext()) {
                    String key = (String) var6.next();
                    builder.addParameter(key, (String) param.get(key));
                }
            }

            URI uri = builder.build();
            HttpPut httpPut = new HttpPut(uri);
            response = httpclient.execute(httpPut);
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception var15) {
            throw var15;
        } finally {
            try {
                if (response != null) {
                    response.close();
                }

                httpclient.close();
            } catch (IOException var14) {
                throw var14;
            }
        }

        return resultString;
    }

}
